# -*- coding: cp936 -*-

import pygame 
from random import randint
from pygame.locals import*
import tkMessageBox

#初始化
pygame.init()

size = width,height = 480,480     #屏幕尺寸

screen = pygame.display.set_mode(size)
black, red, blue, white = (0,0,0), (65,105,225), (0,0,128), (255,255,255) 
empty = 0   #空 

x = -1
o = 1
human = o
board = [[empty] * 3,[empty] * 3,[empty] * 3]    #定义棋盘


def draw_game():
          pygame.draw.rect(screen, (0,255,255), (0, 0, width, height))
          pygame.draw.line(screen, black, (160, 0), (160, 480), 5)
          pygame.draw.line(screen, black, (320, 0), (320, 480), 5)
          pygame.draw.line(screen, black, (0, 160), (480, 160), 5)
          pygame.draw.line(screen, black, (0, 320), (480, 320), 5)

          for row, line in enumerate(board):   
              for col, val in enumerate(line):
                  if val == x:
                      upper_left = (col * 160 + 5, row * 160 + 5)   # X中的  / 
                      lower_right = (col * 160 + 155, row * 160 + 155)  
                      pygame.draw.line(screen, red, upper_left, lower_right, 5)
                      upper_right = (col * 160 + 155, row * 160 + 5)#  X中的 \
                      lower_left = (col * 160 + 5, row * 160 + 155)
                      pygame.draw.line(screen, red, upper_right, lower_left, 5)
                  elif val == o:
                      rect = (col * 160 + 5, row * 160 + 5, 150, 150)
                      pygame.draw.ellipse(screen, blue, rect, 5) 

          pygame.display.flip()

def draw_O():
    while True:
        row = randint(0,2)
        col = randint(0,2)
        if board[row][col] == 0:   
            board[row][col] = 1   #为o
            break
    draw_game()
    pygame.display.flip()

def win():
    for val in range(3):
        if board[0][val] == board[1][val] == board[2][val] != empty:
            return board[0][val]
        if board[val][0] == board[val][1] == board[val][2] != empty:
            return board[val][0]
        if board[0][0] == board[1][1] == board[2][2] != empty:
            return board[1][1]
        if board[0][2] == board[1][1] == board[2][0] != empty:
            return board[1][1]
        return empty
    

def renow():
        global board, turn, human   
        human *= -1          
        board = [[empty] * 3, [empty] * 3, [empty] * 3]  
        turn = x            
        draw_game()          
        pygame.display.flip()  

def EXIT():
          pygame.quit()
          exit(0)

draw_game()

while True:
        event = pygame.event.wait()
        pos = None    #初始化pos
        temp = 0

        
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        elif event.type == KEYDOWN:
            if event.key == K_r:
                renow()
                draw_game()
                pygame.display.flip()

        elif event.type == MOUSEBUTTONDOWN and event.button == 1:

             pos = (event.pos[1] / 160,event.pos[0] / 160) 
             row,col = pos
             board[row][col] = -1
             draw_game()
             for i in range(3):
                 for j in range(3):
                     if board[i][j] == 0:
                         temp += 1
             if temp >= 2:
                 draw_O()

        if win() == -1:
            tkMessageBox.showinfo(title='win',message='You win')
            EXIT()
        elif win() == 1:
            tkMessageBox.showinfo(title='lose',message='You lose')
            EXIT()
                                        
             


                    
                    




# 添加元素  储存  设置出X，O坐标     

#开始游戏

#组成井字棋的函数

#计算玩家移动的函数   电脑走棋

#玩家应移动的函数   电脑计算走棋

#设置游戏输赢条件

#显示输赢

#关闭游戏